package com.cci.kangdao.dao;

import org.apache.ibatis.annotations.*;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;

@MapperScan
public interface GMApplyDao {

	@Select("select COUNT(1) from GMAuthCompany_T where CompanyId = #{companyId} and CompanyType = #{type}")
	public int checkCompany(@Param("companyId") Long companyId, @Param("type") Integer type);
	
	@Select("select CompanyNo from Company_T where ID = #{id}")
	public String getCompanyNo(Long id);
	
	@Select("select StationNo from ServiceStation_T where ID = #{id}")
	public String getStationNo(Long id);

	@Select("select COUNT(1) from GMApplyRecord_T where UserId = #{userId} and Status = 0 and Flag = 0 and RecordType = 0")
	public int checkUserApply0(Long userId);


	
	@Select("select DValue as serviceNo, Remark as serviceName from Dictionaries_T where DTableName = 'GMST' and Flag = 0")
	public List<Map<String, Object>> getGMST();
	
	@Select("select u.ID, u.UserName, u.Phone from User_T u left join UserHasRoles_T uhr on u.ID = uhr.UserID and uhr.Flag = 0\r\n" + 
			"left join UserGroupHasRole_T ughr on uhr.RolesID = ughr.ID and ughr.Flag = 0\r\n" + 
			"left join Roles_T r on ughr.RoleId = r.ID and r.Flag = 0 \r\n" + 
			"where r.RolesNo = 'R0041' and u.Flag = 0 and u.ServiceStationID = #{stationId}")
	public List<Map<String, Object>> getStationAudit(Long stationId);
	
	@Select("select u.ID, u.UserName, u.Phone from GMAuthUser_T au left join User_T u on au.HandlerId = u.ID where au.Flag = 0 and u.Flag = 0 and au.UserGroupId = #{usergroupId}")
	public List<Map<String, Object>> getCompanyAudit(Long usergroupId);
	
	@Insert("INSERT INTO GMApplyRecord_T (UserId ,CompanyId ,CompanyNo ,CompanyName ,CompanyType ,ServiceType ,ServiceUptodateType ,ApplyTime ,Status ,Creator ,CreateTime ) VALUES\r\n" + 
			"           (#{userId}\r\n" + 
			"           ,#{companyId}\r\n" + 
			"           ,#{companyNo}\r\n" + 
			"           ,#{companyName}\r\n" + 
			"           ,#{companyType}\r\n" + 
			"           ,#{serviceType}\r\n" + 
			"           ,#{serviceType}\r\n" + 
			"           ,#{applyTime}\r\n" + 
			"           ,#{status}\r\n" + 
			"           ,#{creator}\r\n" + 
			"           ,#{createTime})")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	int insertApplyRecord(Map<String, Object> map);
	
	@Insert("INSERT INTO GMAuditRecord_T (RecordId,Status,HandlerId,Creator,CreateTime) VALUES\r\n" + 
			"           (#{recordId}\r\n" + 
			"           ,#{status}\r\n" + 
			"           ,#{ID}\r\n" + 
			"           ,#{creator}\r\n" + 
			"           ,#{createTime})")
	int insertAuditRecord(Map<String, Object> map);
	
	@Update("update GMApplyRecord_T set WithdrawTime = #{updateTime}, Status = #{status}, Updator = #{userId}, UpdateTime = #{updateTime}, Version = Version + 1 where UserId = #{userId} and Status = 0 and Flag = 0 and RecordType = #{recordType}")
	int updateApplyRecordStatusToWD(@Param("status") int status, @Param("userId") Long userId, @Param("updateTime") String updateTime, @Param("recordType") Integer recordType);
	
	@Update("update GMApplyRecord_T set DeauthorizeTime = #{updateTime}, DeauthorizeReason=#{reason}, Status = #{status}, Updator = #{userId}, UpdateTime = #{updateTime}, Version = Version + 1 where UserId = #{userId} and Status = 2 and Flag = 0 and RecordType = 0")
	int updateApplyRecordStatusToDA(@Param("status") int status, @Param("userId") Long userId, @Param("updateTime") String updateTime, @Param("reason") String reason);
	
	// 解除用户的权限
	@Update("update GMApplyRecord_T set DeauthorizeTime = #{updateTime}, DeauthorizeReason=#{reason}, Status = 4,ServiceUptodateType='' , Updator = #{userId}, UpdateTime = #{updateTime}, Version = Version + 1 where UserId = #{userId} and Status = 2 and Flag = 0 and RecordType = 0")
	int releaseApplyRecordStatus(@Param("userId") Long userId, @Param("updateTime") String updateTime, @Param("reason") String reason);
	
	
	@Select("select ExpirationTime from ServiceToolOrder_T where ID = #{orderId}")
	String getExpireByOrderId(Long orderId);
	
	@Select("select ar.CompanyId, ar.ServiceType, ar.OrderId, u.ID, u.UserName, u.Phone, u.Type from GMApplyRecord_T ar left join User_T u on ar.UserId = u.ID where ar.ID = #{recordId}")
	Map<String, Object> getApplyUserInfo(Long recordId);
	
	@Update("update ServiceToolOrder_T set UsableGMNum = UsableGMNum - 1, HasBeenUsedGMNum = HasBeenUsedGMNum + 1 where ID = #{orderId} and UsableGMNum > 0")
	int occupyOneResause(Long orderId);
	
	@Update("update ServiceToolOrder_T set UsableGMNum = UsableGMNum + 1, HasBeenUsedGMNum = HasBeenUsedGMNum - 1 where ID = #{orderId}")
	int releaseOneResause(Long orderId);
	
	@Select("select OrderId from GMApplyRecord_T where UserId = #{userId} and Status = 4 and OrderId is not null")
	List<Long> getOccupiedOrderId(Long userId);
	
	/**
	 * 获取超时未审核相关数据
	 * @param current
	 * @param period
	 * @return
	 */
	@Select("select ar.ID, audit.ID as auditId, audit.UserName as auditName, audit.Phone as auditPhone, apply.ID as applyId, apply.Contact as applyName, apply.Phone as applyPhone from GMApplyRecord_T ar left join GMAuditRecord_T aur on ar.Status = aur.Status and ar.ID = aur.RecordId\r\n" + 
			"left join User_T audit on aur.HandlerId = audit.ID left join User_T apply on ar.UserId = apply.ID\r\n" + 
			" where DATEDIFF(DAY, ar.ApplyTime, #{current}) = #{period} and ar.Status = 0")
	List<Map<String, Object>> getOverTimeList(@Param("current") String current, @Param("period") String period);
	
	/**
	 * 获取已过期人员列表
	 * @param current
	 * @return
	 */
	@Select("select ar.ID, u.ID as applyId, u.UserName as applyName, u.Phone as applyPhone, u.Type from GMApplyRecord_T ar inner join User_T u on ar.UserId = u.ID where ar.ExpireDate BETWEEN #{current} AND CONVERT(varchar(100),dateadd(second, -1, dateadd(day, 1,#{current})), 21)  and ar.Status = 2 and u.Flag = 0")
	List<Map<String, Object>> getOverTimeUserList(String current);
	
	/**
	 * 修改已审核状态的申请记录为已过期
	 * @param current
	 * @return
	 */
	@Update("update GMApplyRecord_T set Status = 5, Version = Version + 1 where ExpireDate = #{current} and Status = 2")
	int updateOverTimeFlag(String current);


	/**
	 * 查询上一个工作日，且状态为未审批的GM申请记录
	 * @return Id数组
	 */
	@Select("select ga.ID as recordId from GMApplyRecord_T ga where ga.Status = 0 and Flag = 0")
	List<String> getGMDailyStatus();


	/**
	 * 根据userId从查询orderid
	 * @param userId
	 * @return
	 */
	@Select("select orderId from GMApplyRecord_T a where a.UserId = #{userId} and Flag = 0 and Status = 2")
	String getOrderId(long userId);

	@Select("select UsableGMNum from ServiceToolOrder_T where ID = #{orderId} and Flag = 0")
	int getGMUsableNum(String orderId);
	
	@Select("select top 1 * from GMApplyRecord_T where flag = 0 and status = 2 and recordType = 0 and userId = #{userId}")
	Map<String, Object> getUserApplyRecordByUserId(@Param("userId") Long userId);
	
	@Select("select COUNT(1) from GMApplyRecord_T where UserId = #{userId} and Status = 2 and Flag = 0 and RecordType = 0")
	public int checkUserApply2(Long userId);
	
	
	int updateApplyRecordByIdAndVersion(Map<String, Object> map);
}
